package com.zone.service.controller;

import java.util.List;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zone.common.log.annotation.Log;
import com.zone.common.log.enums.BusinessType;
import com.zone.common.security.annotation.RequiresPermissions;
import com.zone.service.domain.GunSecurity;
import com.zone.service.service.IGunSecurityService;
import com.zone.common.core.web.controller.BaseController;
import com.zone.common.core.web.domain.AjaxResult;
import com.zone.common.core.utils.poi.ExcelUtil;
import com.zone.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 保安员证信息Controller
 * 
 * @author zone
 * @date 2025-11-06
 */
@RestController
@RequestMapping("/gunSecurity")
public class GunSecurityController extends BaseController
{
    @Autowired
    private IGunSecurityService gunSecurityService;

    /**
     * 查询保安员证信息列表
     */
    @RequiresPermissions("service:gunSecurity:list")
    @GetMapping("/list")
    public TableDataInfo list(GunSecurity gunSecurity)
    {
        startPage();
        List<GunSecurity> list = gunSecurityService.selectGunSecurityList(gunSecurity);
        return getDataTable(list);
    }

    @RequiresPermissions("service:gunSecurityQuit:quit")
    @GetMapping("/quit/list")
    public TableDataInfo quit(GunSecurity query)
    {
        startPage();
        List<GunSecurity> list = gunSecurityService.selectQuitSecurityList(query);
        return getDataTable(list);
    }

    /**
     * 导出保安员证信息列表
     */
    @RequiresPermissions("service:gunSecurity:export")
    @Log(title = "保安员证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GunSecurity gunSecurity)
    {
        List<GunSecurity> list = gunSecurityService.selectGunSecurityList(gunSecurity);
        ExcelUtil<GunSecurity> util = new ExcelUtil<GunSecurity>(GunSecurity.class);
        util.exportExcel(response, list, "保安员证信息数据");
    }

    /**
     * 获取保安员证信息详细信息
     */
    @RequiresPermissions("service:gunSecurity:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gunSecurityService.selectGunSecurityById(id));
    }

    /**
     * 上传证件图片
     */
    @RequiresPermissions("service:gunSecurity:upload")
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return error("上传文件不能为空");
        }

        // 验证文件大小（5MB = 5 * 1024 * 1024 字节）
        long maxSize = 5 * 1024 * 1024;
        if (file.getSize() > maxSize) {
            return error("文件大小不能超过 5MB");
        }

        // 验证文件类型
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
        if (!"png".equals(ext) && !"jpg".equals(ext) && !"jpeg".equals(ext)) {
            return error("仅支持 png/jpg/jpeg 格式文件");
        }

        try {
            String fileName = System.currentTimeMillis() + "_" + originalFilename;
            String filePath = "D:/uploads/" + fileName;

            java.io.File dest = new java.io.File(filePath);
            file.transferTo(dest);

            AjaxResult result = success("上传成功");
            result.put("filePath", filePath);
            return result;
        } catch (Exception e) {
            return error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 新增保安员证信息
     */
    @RequiresPermissions("service:gunSecurity:add")
    @Log(title = "保安员证信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GunSecurity gunSecurity)
    {
        return toAjax(gunSecurityService.insertGunSecurity(gunSecurity));
    }

    /**
     * 修改保安员证信息
     */
    @RequiresPermissions("service:gunSecurity:edit")
    @Log(title = "保安员证信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GunSecurity gunSecurity)
    {
        return toAjax(gunSecurityService.updateGunSecurity(gunSecurity));
    }

    /**
     * 删除保安员证信息
     */
    @RequiresPermissions("service:gunSecurity:remove")
    @Log(title = "保安员证信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gunSecurityService.deleteGunSecurityByIds(ids));
    }
}
